package cn.iocoder.yudao.module.wine.service.agreement;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.wine.controller.admin.agreement.enums.AgreementEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.wine.controller.admin.agreement.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.agreement.AgreementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wine.dal.mysql.agreement.AgreementMapper;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 协议配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class AgreementServiceImpl implements AgreementService {

    @Resource
    private AgreementMapper agreementMapper;

    @Override
    public Long createAgreement(AgreementSaveReqVO createReqVO) {
        // 插入
        AgreementDO agreement = BeanUtils.toBean(createReqVO, AgreementDO.class);
        if (AgreementEnum.REGISTRATION_AGREEMENT.getCode().equals(createReqVO.getType())) {
            AgreementDO agreementDO = agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>().eq(AgreementDO::getType, AgreementEnum.REGISTRATION_AGREEMENT.getCode()));
            if (agreementDO == null){
                agreementMapper.insert(agreement);
                // 返回
                return agreement.getId();
            }
        }else if (AgreementEnum.SUPPLY_AGREEMENT.getCode().equals(createReqVO.getType())){
            AgreementDO agreementDO = agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>().eq(AgreementDO::getType, AgreementEnum.SUPPLY_AGREEMENT.getCode()));
            if (agreementDO == null){
                agreementMapper.insert(agreement);
                // 返回
                return agreement.getId();
            }
        }else {
            AgreementDO agreementDO = agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>().eq(AgreementDO::getType, AgreementEnum.VIP_AGREEMENT.getCode()));
            if (agreementDO == null){
                agreementMapper.insert(agreement);
                // 返回
                return agreement.getId();
            }
        }
        throw exception(AGREEMENT_REPETITION);

    }

    @Override
    public void updateAgreement(AgreementSaveReqVO updateReqVO) {
        // 校验存在
        AgreementDO agreementDO = validateAgreementExists(updateReqVO.getId());
        if (!agreementDO.getType().equals(updateReqVO.getType())) {
            AgreementDO data = agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>().eq(AgreementDO::getType, updateReqVO.getType()));
            if (data != null){
                throw exception(AGREEMENT_EXISTS);
            }
        }
        // 更新
        AgreementDO updateObj = BeanUtils.toBean(updateReqVO, AgreementDO.class);
        agreementMapper.updateById(updateObj);
    }

    @Override
    public void deleteAgreement(Long id) {
        // 校验存在
        validateAgreementExists(id);
        // 删除
        agreementMapper.deleteById(id);
    }

    private AgreementDO validateAgreementExists(Long id) {
        AgreementDO agreementDO = agreementMapper.selectById(id);
        if (agreementDO == null) {
            throw exception(INFO_NOT_EXISTS);
        }
        return agreementDO;
    }

    @Override
    public AgreementDO getAgreement(Long id) {
        return agreementMapper.selectById(id);
    }

    @Override
    public PageResult<AgreementDO> getAgreementPage(AgreementPageReqVO pageReqVO) {
        return agreementMapper.selectPage(pageReqVO);
    }

    @Override
    public AgreementDO getSupplyAgreement() {
        return agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>()
                .eq(AgreementDO::getType, AgreementEnum.SUPPLY_AGREEMENT.getCode()).last("limit 1"));
    }

    @Override
    public AgreementDO getRegisterAgreement() {
        return agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>()
                .eq(AgreementDO::getType, AgreementEnum.REGISTRATION_AGREEMENT.getCode()).last("limit 1"));

    }

    @Override
    public AgreementDO getVipAgreement() {
        return agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>()
                .eq(AgreementDO::getType, AgreementEnum.VIP_AGREEMENT.getCode()).last("limit 1"));


    }

    @Override
    public AgreementDO getPosterAgreement() {
        return agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>()
                .eq(AgreementDO::getType, AgreementEnum.POSTER.getCode()).last("limit 1"));


    }

    @Override
    public AgreementDO appGetAgreement(Integer type) {
        return agreementMapper.selectOne(new LambdaQueryWrapperX<AgreementDO>()
                .eq(AgreementDO::getType, type).last("limit 1"));

    }

}
